package com.neuedu.web;

import com.neuedu.pojo.AdminTable;
import com.neuedu.pojo.CadreTable;
import com.neuedu.pojo.PoorFamily;
import com.neuedu.pojo.Result;
import com.neuedu.service.AdminTableService;
import com.neuedu.service.CadreTableService;
import com.neuedu.service.PoorFamilyService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
@Tag(name = "登录校验")
public class LoginController {
    @Resource
    private CadreTableService cadreTableService;
    @Resource
    private PoorFamilyService poorFamilyService;
    @Resource
    private AdminTableService adminTableService;

    @GetMapping("/loginCadreTable")
    public Result<CadreTable> login(@RequestParam(name = "username") String number,
                                    @RequestParam(name = "password") String password) {
        return cadreTableService.loginCadre(number, password);
    }

    @GetMapping("/loginPoorFamily")
    public Result<PoorFamily> loginPoorFamily(@RequestParam(name = "username") String number,
                                              @RequestParam(name = "password") String password
                                              ) {
        return poorFamilyService.loginPfamily(number, password);
    }
    @GetMapping("/loginAdmin")
    public Result<AdminTable> loginAdmin(@RequestParam(name = "username") String number,
                                         @RequestParam(name = "password") String password
    ) {
        return adminTableService.loginAdmin(number, password);
    }

    @GetMapping("/updatePassword")
    public Result<Void> updatePassword(@RequestParam(name="username")String number,
                                       @RequestParam(name="passwords")String passwords,
                                       @RequestParam(name="phone")String phone,
                                       @RequestParam(name="password")String password){
        char firstChar = 0;
        if (number != null && number.length() > 0) {
            firstChar = number.charAt(0);
        } else {
            return Result.fail();
        }
        if (firstChar == 'p' || firstChar == 'P') {
            return poorFamilyService.updatePassword(number,password);
        } else if (firstChar == 'c' || firstChar == 'C') {
            return cadreTableService.updatePassword(number,password);
        } else {
            return Result.fail();
        }

    }
}
